﻿using System.Xml.Linq;
using Xunit;
using Xunit.Should;

namespace Adjunct.Framework.Nuspec
{
	public class NuspecDependenciesSpec
	{
		private const string ValidXml =
			"<dependencies><dependency id='Adjunct.Framework' /><dependency id='xunit' version='1.8' /></dependencies>";

		private readonly NuspecDependency _dependency1 = new NuspecDependency("xunit", "1.8");
		private readonly NuspecDependency _dependency2 = new NuspecDependency("Adjunct.Framework");

		[Fact]
		public void EmptyToXElement()
		{
			NuspecDependencies dependencies = new NuspecDependencies();
			((XElement)dependencies).ShouldBeNull();
		}

		[Fact]
		public void FromEmptyXElement()
		{
			XElement element = XElement.Parse("<dependencies />");
			NuspecDependencies dependencies = ((NuspecDependencies)element);
			dependencies.ShouldNotBeNull();
			dependencies.ShouldBeEmpty();
		}

		[Fact]
		public void FromNullXElement()
		{
			XElement element = null;
			NuspecDependencies dependencies = ((NuspecDependencies)element);
			dependencies.ShouldNotBeNull();
			dependencies.ShouldBeEmpty();
		}

		[Fact]
		public void FromValidXElement()
		{
			XElement element = XElement.Parse(ValidXml);
			((NuspecDependencies)element).ShouldEqual(new NuspecDependencies {
				_dependency1,
				_dependency2
			});
		}

		[Fact]
		public void NullToXElement()
		{
			NuspecDependencies dependencies = null;
			((XElement)dependencies).ShouldBeNull();
		}

		[Fact]
		public void ValidToXElement()
		{
			NuspecDependencies dependencies = new NuspecDependencies {
				_dependency1,
				_dependency2
			};
			((XElement)dependencies).ShouldEqual(ValidXml);
			dependencies = new NuspecDependencies {
				_dependency2,
				_dependency1
			};
			((XElement)dependencies).ShouldEqual(ValidXml);
		}
	}
}